#include "FileReceiverServer.h"
#include "FileReceiver.h"

FileReceiverServer::FileReceiverServer(QObject* parent)
	:QTcpServer(parent)
{

}

FileReceiverServer::~FileReceiverServer()
{

}

void FileReceiverServer::setSavePaths(const QList<FileTransferDef::sRevSavePath>& revSavePaths)
{
    mSavePaths = revSavePaths;
}

void FileReceiverServer::incomingConnection(qintptr socketDescriptor)
{
	FileReceiver* fileReceiver = getFileReceiverManager()->createReceiver(socketDescriptor, mSavePaths);
    if (fileReceiver) {
        QString ip = fileReceiver->getIp();
        connect(fileReceiver, &FileReceiver::signalFileReceiveFinished, this, [=](const QString& filePath, const QByteArray& userData) {
            emit signalFileReceiveFinished(ip, filePath, userData);
        });
        connect(fileReceiver, &FileReceiver::signalReceiveProgressChanged, this, [=](int percentage) {
            emit signalReceiveProgressChanged(ip, percentage);
        });
        connect(fileReceiver, &FileReceiver::signalReceiveError, this, [=](FileTransferDef::eRecevieError e) {
            emit signalReceiveError(ip, e);
        });
        connect(fileReceiver, &FileReceiver::signalReceiveStart, this, [=]() {
            emit signalReceiveStart(ip);
        });
        connect(fileReceiver, &FileReceiver::signalClose, this, [=]() {getFileReceiverManager()->destroyReceiver(fileReceiver); });
    }
}
